package com.hotel.controller;

import com.hotel.mapper.auto.RoomMapper;
import com.hotel.model.auto.Orders;
import com.hotel.service.IOrdersService;
import com.hotel.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2023-04-03
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private IOrdersService iOrdersService;

    @Resource
    private RoomMapper roomMapper;

    /*查询订单*/
    @GetMapping("/getOrders")
    public List<Orders> getOrders() {
        return iOrdersService.list();
    }

    /*通过id查询订单*/
    @GetMapping("/getOrdersById/{id}")
    public Orders getOrdersById(@PathVariable("id") int id) {
        return iOrdersService.getById(id);
    }

    /*修改订单*/
    @PostMapping("/updateOrders")
    public void updateOrders(@RequestBody Orders Orders) {
        iOrdersService.updateById(Orders);
    }

    /*添加订单*/
    @PostMapping("/setOrders")
    public void setOrders(@RequestBody Orders Orders) {
        iOrdersService.save(Orders);
    }

    /*删除订单*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id) {
        Orders orders = new Orders();
        Orders byId = iOrdersService.getById(id);
        String room = byId.getRoomId();
        int roomId = Integer.parseInt(room);
        System.out.println(roomId);
        roomMapper.PayUpE(roomId);
        return iOrdersService.removeById(id);
    }

    /*批量删除订单*/
    @PostMapping("/deletedAllById")
    public boolean deletedAllById(@RequestBody int[] delarr) {
        for (int i : delarr) {
            Orders orders = new Orders();
            Orders byId = iOrdersService.getById(i);
            String room = byId.getRoomId();
            int roomId = Integer.parseInt(room);
            System.out.println(roomId);
            roomMapper.PayUpE(roomId);
            System.out.println(i);
            iOrdersService.removeById(i);
        }
        return true;
    }

}
